package paytreasure.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import paytreasure.po.Consume;
import paytreasure.service.ConsumeService;
import paytreasure.util.ConstantUtil;
import paytreasure.util.ObjAnalysis;
import paytreasure.vo.ResultVo;

/**
 * 
 * @author xhh
 * @date 
 * 
 */
@Controller
@RequestMapping(value="/consume")
public class ConsumeController {

	private static final Logger LOGGER = LoggerFactory.getLogger(ConsumeController.class);
	
	@Resource
	private ConsumeService consumeService ;
	
	private ResultVo result;


	/**
	 * 
	 * @param 
	 * @return 添加保存
	 * @return_type: String
	 *
	 */
    @RequestMapping(value = "/insertConsume.action", method = {RequestMethod.POST},produces = "text/html;charset=UTF-8")
    @ResponseBody
	public String insertConsume(@RequestBody Map<String, Object> map){

    	Consume con = new Consume();
    	
    	try {
    		//BeanUtils.populate(con, map);
    		ObjAnalysis.transMap2Bean(map, con);
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("转换异常");
		}
    	
    	con.setConsumeId(UUID.randomUUID().toString());
		con.setCreateTime(new Date());
		result = consumeService.insertConsume(con);
		return JSONObject.toJSONString(result, ConstantUtil.vFilter);
	}

    /**
	 * 
	 * @param 
	 * @return  详情页面
	 * @return_type: String
	 *
	 */
	@RequestMapping(value="/toConsumeMessage.action", method={RequestMethod.POST},produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String toConsumeMessage(@RequestBody Map<String, String> map) throws Exception {
		result = consumeService.getConsumeById(map.get("consumeId"));
		return JSONObject.toJSONString(result, ConstantUtil.vFilter);
	}
	
	/**
	 * 
	 * @param id
	 * @return保存修改页面
	 *
	 */
	@RequestMapping(value="/saveEditConsume.action", method={RequestMethod.POST},produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String saveEditConsume(@RequestBody HashMap<String, Object> map) {
		
		Consume con = new Consume();
		try {
    		ObjAnalysis.transMap2Bean(map, con);
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("类转换异常");
		}
		con.setUpdateTime(new Date());//修改时间
		consumeService.updateConsume(con);
		result = new ResultVo();
		result.isResultSuccess();
		result.setResultObj(con);
		return JSONObject.toJSONString(result, ConstantUtil.vFilter);
	}
	
		
	 /**
	  * 
	  * @param 
	  * @return
	  * @return_type: void
	  *
	  */
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

}